package com.xxx.video.mine.adapters;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.adapters.BaseViewHolder;
import com.xxx.video.commen.dbs.beans.DownloadHistoryBean;
import com.xxx.video.commen.utils.CountUtils;
import com.xxx.video.commen.utils.DimensionUtil;
import com.xxx.video.commen.utils.DownloadUtils;
import com.xxx.video.commen.utils.ResourcesUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.views.ProgressView;

import butterknife.BindView;

public class DownloadManagementListAdapter extends BaseRecycleViewAdapter<DownloadHistoryBean, DownloadManagementListAdapter.MyHolder> {


    public DownloadManagementListAdapter(@NonNull Context context) {
        super(context);
    }

    @Override
    public MyHolder onCreateDefaultViewHolder(View view, int viewType) {
        return new MyHolder(view);
    }

    @Override
    public int getItemLayout(int viewType) {
        return R.layout.item_download_management_list;
    }

    class MyHolder extends BaseViewHolder {


        @BindView(R.id.iv_item_download_management_cover)
        ImageView ivItemDownloadManagementCover;
        @BindView(R.id.tv_item_download_management_title)
        TextView tvItemDownloadManagementTitle;
        @BindView(R.id.tv_item_download_management_play_count)
        TextView tvItemDownloadManagementPlayCount;
        @BindView(R.id.tv_item_download_management_comment_count)
        TextView tvItemDownloadManagementCommentCount;
        @BindView(R.id.tv_item_download_management_time)
        TextView tvItemDownloadManagementTime;
        @BindView(R.id.tv_item_download_management_progress)
        ProgressView tvItemDownloadManagementProgress;


        public MyHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void refresh(int progress) {
            super.refresh(progress);
            if (tvItemDownloadManagementProgress != null) {
                tvItemDownloadManagementProgress.setProgress(progress);
            }
        }

        @Override
        public void setStatus(String url) {
            super.setStatus(url);
            DownloadUtils.getInstance().setStatus(tvItemDownloadManagementProgress, url, this);
        }

        @Override
        public void setData2View(int position) {
            DownloadHistoryBean listBean = getItem(position);
            /*
             * cover
             */
            Glide.with(context).load(ResourcesUtils.getInstance().getImagePath(listBean.fPicurl))
                    .apply(new RequestOptions()
                            .error(R.drawable.bg_place_holder_nine_five)
                            .placeholder(R.drawable.bg_place_holder_nine_five)
                            .transform(new CenterCrop(), new RoundedCorners(DimensionUtil.dpToPx(5))))
                    .into(ivItemDownloadManagementCover);
            /*
             * time
             */
            tvItemDownloadManagementTime.setText(StringUtils.getInstance().getNotEmptyMsg(String.valueOf(listBean.fCreateTime)));
            /*
             * title
             */
            tvItemDownloadManagementTitle.setText(StringUtils.getInstance().getNotEmptyMsg(listBean.fTitle));
            /*
             * play count
             */
            tvItemDownloadManagementPlayCount.setText(CountUtils.getInstance().getCount((int) listBean.fPlayTimes));
            /*
             *comment count
             */
            tvItemDownloadManagementCommentCount.setText(CountUtils.getInstance().getCount((int) listBean.fCommentTimes));
            String url = ResourcesUtils.getInstance().getDownloadPath(listBean.fVideo);
            setTag(url);
            DownloadUtils.getInstance().setStatus(tvItemDownloadManagementProgress, url, this);
        }
    }
}
